﻿using RLRT.Common;
using RLRT.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace RLRT.Web
{
    public partial class OrderPick : LocalPage
    {
        /// <summary>
        /// 页面加载
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                #region 验证权限

                #endregion

                #region 获取参数

                #endregion

                //初始化
                this.Initialize();
                this.BindList();
            }

            #region 页面标题

            this.Title = PageTitle.Text;

            #endregion

            #region 页面要素


            #endregion
        }

        #region 初始化

        /// <summary>
        /// 初始化
        /// </summary>
        private void Initialize()
        {
            #region 页面内容
            using (RTProvider rtProvider = new RTProvider())
            {
                //var ornos = rtProvider.GetPickOrNoList();
                //LocalGlobal.BindListItems(ORDNO, ornos.DefaultView, "bomhwno", "bomhwno", true);

                var whs = rtProvider.GetWarehouseList();
                LocalGlobal.BindListItems(WhCode, whs.DefaultView, "wmdes", "wmwhs", true);
            }

            this.ORDNO.Text = string.Empty;
            this.DrawNo.Text = string.Empty;
            this.OrQty.Text = string.Empty;
            this.PickNo.Text = string.Empty;
            this.PickDate.Text = DateTime.Now.ToString("yyyy-MM-dd");

            #endregion

            //绑定列表
            //this.BindList();
        }

        #endregion

        #region 绑定数据

        protected void ORDNO_TextChanged(object sender, EventArgs e)
        {
            if (this.ORDNO.Text.Trim().Length > 0)
            {
                using (RTProvider rtProvider = new RTProvider())
                {
                    var bomhead = rtProvider.GetBOMHeader(this.ORDNO.Text.Trim());
                    if (bomhead == null)
                    {
                        this.DrawNo.Text = string.Empty;
                        this.OrQty.Text = string.Empty;
                        return;
                    }
                    this.DrawNo.Text = bomhead["bomhmno"].ToString();
                    this.OrQty.Text = bomhead["bomhqre"].ToString();
                }
            }
        }

        /// <summary>
        /// 绑定列表
        /// </summary>
        private void BindList()
        {
            this.CreateTable();
            this.BindTempData();
        }

        #endregion

        #region 操作

        /// <summary>
        /// 删除List临时行
        /// </summary>
        protected void DeleteButton_Click(object sender, EventArgs e)
        {
            DataTable table = LocalGlobal.tmpTable;

            bool deleted = false;
            foreach (GridViewRow row in List.Rows)
            {
                HtmlInputCheckBox rowCheckControl = (HtmlInputCheckBox)row.FindControl("RowCheck");
                if (rowCheckControl.Checked)
                {
                    var seq = row.Cells[1].Text.Trim(); //序号
                    var prltno = row.Cells[2].Text.Trim(); //工件号

                    foreach (DataRow dr in table.Rows)
                    {
                        if (dr["bomseq"].ToString().Trim() == seq && dr["bompno"].ToString().Trim() == prltno)
                        {
                            table.Rows.Remove(dr);
                            break;
                        }
                    }
                    //有项被删除
                    deleted = true;
                }
            }
            if (deleted)
            {
                LocalGlobal.tmpTable = table;
                BindTempData();
            }
            else
            {
                this.ShowInfoMessage(this.GetGlobalResourceString("NotSelectMessage"));
            }
        }

        /// <summary>
        /// 弹出选择工件回调刷新列表数据
        /// </summary>
        protected void RefreshButton_Click(object sender, EventArgs e)
        {
            this.BindTempData();
        }

        /// <summary>
        /// 保存
        /// </summary>
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            SqlConnection con = LocalGlobal.DbConnect();
            con.Open();
            SqlTransaction tran = con.BeginTransaction();//使用事务
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            cmd.Transaction = tran;

            try
            {
                using (RTProvider rtProvider = new RTProvider())
                {
                    bool flag = false;
                    string orNo = this.ORDNO.Text.Trim();
                    string whCode = this.WhCode.SelectedValue.Trim();
                    string pickNo = this.PickNo.Text.Trim();
                    string pickDate = this.PickDate.Text.Trim();
                    int seq = 0;
                    foreach (GridViewRow row in List.Rows)
                    {
                        seq++;
                        string pickqty = ((TextBox)row.FindControl("pickqty")).Text.Trim(); //领用数量

                        //新增领用记录
                        cmd.CommandText = rtProvider.InsertPickSql(pickNo, seq.ToString(), whCode, orNo, row.Cells[1].Text.Trim(), row.Cells[2].Text.Trim(), row.Cells[3].Text.Trim(), row.Cells[5].Text.Trim(), row.Cells[4].Text.Trim(), pickqty, row.Cells[6].Text.Trim(), pickDate, "");
                        cmd.ExecuteNonQuery();

                        //更新库存数量
                        cmd.CommandText = rtProvider.DecreaseInventorySql(whCode, row.Cells[2].Text.Trim(), pickqty);
                        cmd.ExecuteNonQuery();

                        flag = true;
                    }

                    if (!flag)
                    {
                        this.ShowInfoMessage(this.GetGlobalResourceString("NotSelectMessage"));
                        tran.Rollback();
                        return;
                    }
                }
            }
            catch (Exception error)
            {
                tran.Rollback();
                this.ShowErrorMessage("操作失败。" + error.Message);
                return;
            }

            tran.Commit();

            Initialize();
            this.BindList();
        }

        /// <summary>
        /// 取消
        /// </summary>
        protected void CancelButton_Click(object sender, EventArgs e)
        {
            this.Initialize();
        }

        #endregion

        #region 创建表结构及绑定
        protected void CreateTable()
        {
            //定义table结构   
            LocalGlobal.tmpTable = new DataTable();
            DataTable dt1 = LocalGlobal.tmpTable;
            //不设置 默认为System.String  
            dt1.Columns.Add("bomseq");
            dt1.Columns.Add("bompno");
            dt1.Columns.Add("bomnam");
            dt1.Columns.Add("bommat");
            dt1.Columns.Add("bomspc");
            dt1.Columns.Add("bomuit");
            dt1.Columns.Add("bomreq");
            dt1.Columns.Add("pickqty");

            //dt1.Rows.Add(dt1.NewRow());
            //ViewState["ViewDT"] = dt1;
            //return dt1;
        }

        protected void BindTempData()
        {
            this.List.DataSource = LocalGlobal.tmpTable;
            this.List.DataBind();
        }

        #endregion

    }
}